package br.com.financeiro.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import br.com.financeiro.utils.Role;

@Entity
@Table(name="usuario")
@NamedQuery(name="Usuario.findUserByEmail",query="Usuario u From Usuario u Where u.email =: email")
public class Usuario implements Serializable {

	/**
	 * serial
	 */
	private static final long serialVersionUID = 1L;

	public static final String FIND_BY_EMAIL="Usuareio.findUserByEmail"; 
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Long id;
	
	@Column(name= "email", unique=true, nullable=false, length=40)
	private String email;
	
	@Column(name="nome", nullable=false, length=30)
	private String nome;
	
	@Column(name="senha", nullable=false, length=10)
	private String senha;
	
	@Enumerated(EnumType.STRING)
	private Role role;
	
	public Usuario(){
		
	}

	public boolean idAdmin(){
		return Role.ADMIN.equals(role);
	}
	
	public boolean idUser(){
		return Role.USER.equals(role);
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public String getSenha() {
		return senha;
	}

	public void setSenha(String senha) {
		this.senha = senha;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Usuario other = (Usuario) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
}
